#include "matrixLCD.h"

/*
*  GPO_ON and GPO_OFF are helper functions for LED switching
*/
void GPO_ON(int);
void GPO_OFF(int);

void initLCD()
{
	Serial1.begin(19200);
	
	//Turn Auto scroll OFF
	Serial1.write(254);
	Serial1.write(82); 
	
	//Turn OFF AUTO line wrap
	Serial1.write(254); 
	Serial1.write(68); 
	
	clearLCD();
}

/*
* prints to serial line of lcd
*/
void lcdPrintln(String out){
        clearLCD();
	Serial1.print(out);
	newLine();
}

/*
* prints to lcd
*/
void lcdPrint(String out){
	Serial1.print(out);
}

/*
* clears screen and displays startup msg
*/
void introLCD()
{
	clearLCD();
	cursorHome();
	Serial1.print("Welcome to...");
	newLine();
	Serial1.print("MAGCHESS!!");
}

/*
* gets players promotion choice
*/
Piecetype getPromotionChoice(Color player)
{
	int button;
	Piecetype piece=QUEEN;

	clearLCD();
	cursorHome();
	if(player == WHITE)
		Serial1.print("WHITE PLAYER:");
	else
		Serial1.print("BLACK PLAYER:");
	newLine();
	Serial1.print("Choose a piece to promote to:");
	newLine();
	Serial1.print("QUEEN");

	for(;;)
	{
		/*
		*  waits for button press
		*/
		while(Serial1.available() <= 0)
			delay(100);
		button = Serial1.read();

		/*
		* Handles switching between available choices
		*/
		if(button == LEFT_ARROW) {
			switch(piece) {
  			  case QUEEN:
  			  piece = KNIGHT;
  			  break;
  			  case KNIGHT:
  			  piece = QUEEN;
  			  break;
  		     }
		} else if (button == RIGHT_ARROW) {
			switch(piece) {
  			  case QUEEN:
  			  piece = KNIGHT;
  			  break;
  			  case KNIGHT:
  			  piece = QUEEN;
  			  break;
  		        }
		} else if (button == CENTER_BUTTON)
			return piece;
		else {continue;}

		cursorSet(0, 3);
		switch(piece) {
			case QUEEN:
			Serial1.print("QUEEN ");
			break;
			case KNIGHT:
			Serial1.print("KNIGHT");
			break;
		}
	}
}


/*
 *Returns movetype based on button pressed
 */
Movetype get_button() {
    
    int button;
    
    while (1) {
      
      while(Serial1.available() <= 0)
          {};
      
      button = Serial1.read();
      
      if(button  == LEFT_ARROW) {
          //Serial.println("User hit Top Left Button");
          return CASTLING;
      }
          
      else if(button == CENTER_BUTTON) {
          //Serial.println("User hit Center Button");
          return NEWMOVE;
      }
    }       
}






/*
*  sets the state of led1 (top)
*			GPO2	GPO1
*	Green	0		1
*	Red	1		0
*	Off	1		1
*/
void led1(int state){
	switch(state){
		case OFF:
			GPO_ON(2);
			GPO_ON(1);
		break;

		case RED:
			GPO_ON(2);
			GPO_OFF(1);
		break;

		case GREEN:
			GPO_OFF(2);
			GPO_ON(1);
		break;

		default: //invalid input, do nothing
		break;
	}
}

/*
* sets state of led2 (middle)
*			GPO4	GPO3
*	Green	0		1
*	Red	1		0
*	Off	1		1
*/
void led2(int state){
	switch(state){
		case OFF:
			GPO_ON(4);
			GPO_ON(3);
		break;

		case RED:
			GPO_ON(4);
			GPO_OFF(3);
		break;

		case GREEN:
			GPO_OFF(4);
			GPO_ON(3);
		break;

		default: //invalid input, do nothing
		break;
	}
}

/*
* sets state of led3 (bottom)
*			GPO6	GPO5
*	Green	0		1
*	Red	1		0
*	Off	1		1
*/
void led3(int state){
	switch(state){
		case OFF:
			GPO_ON(6);
			GPO_ON(5);
		break;

		case RED:
			GPO_ON(6);
			GPO_OFF(5);
		break;

		case GREEN:
			GPO_OFF(6);
			GPO_ON(5);
		break;
		
		default: //invalid input, do nothing
		break;
	}
}

/*
* Helper function for LED
*/
void GPO_ON(int GPO){
	if((GPO<1) || (GPO>6)) //invalid input
		return;
	Serial1.write(254);
	Serial1.write(87);
	Serial1.write(GPO);
}

/*
* Helper function for LED
*/
void GPO_OFF(int GPO){
	Serial1.write(254);
	Serial1.write(86);
	Serial1.write(GPO);
}

/*
* clears screen
*/
void clearLCD(){
  Serial1.write(254);
  Serial1.write(88);
}

/*
* prints newline
*/
void newLine() { 
  Serial1.write(0x0a); 
}

/*
* moves cursor to top left
*/
void cursorHome(){
  Serial1.write(254);
  Serial1.write(72);
}

/*
* moves cursor to coordinate
*/
void cursorSet(int xpos, int ypos){  
  Serial1.write(254);
  Serial1.write(71);               
  Serial1.write(xpos); //Column position   
  Serial1.write(ypos); //Row position 
} 
